#coding=utf-8
import numpy as np

import pandas as pd
import seaborn as sns

#import matplotlib.pyplot as plt

from sklearn.cross_validation import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import r2_score

from sklearn.linear_model import RidgeCV

from sklearn.preprocessing import StandardScaler

data_path = "C:/Users/Administrator/Documents/Tencent Files/604486216/FileRecv/"

#data = pd.read_csv(data_path + "boston_house_prices.csv")

data = pd.read_csv(data_path + "day.csv")

#获取2011年训练数据
data_train = data[data.yr < 1 ]

#获取2012年测试数据
data_test = data[data.yr > 0 ]
#instant  dteday  season  yr  mnth  holiday  weekday  workingday

#得到x,y训练集
x_train = data_train[['season','yr','mnth','holiday','weekday','workingday','weathersit','atemp','hum','windspeed']]

y_train = data_train[['cnt']]

#得到x,y 测试集
x_test = data_test[['season','yr','mnth','holiday','weekday','workingday','weathersit','atemp','hum','windspeed']]

y_test = data_test[['cnt']]



#数据标准化
ss_x = StandardScaler()

ss_y = StandardScaler()


x_train = ss_x.fit_transform(x_train)

x_test = ss_x.fit_transform(x_test)


y_train = ss_y.fit_transform(y_train.values.reshape(-1,1))

y_test = ss_y.fit_transform(y_test.values.reshape(-1,1))


#无正则的线性回归
def fun_OLS():
    lr = LinearRegression()
    lr.fit(x_train, y_train)
    y_test_pred_lr = lr.predict(x_test)
    y_train_pred_lr = lr.predict(x_train)
    #测试集
    print "the r2 score  LinearRegression on test is:", r2_score(y_test, y_test_pred_lr)
    #训练集
    print "the r2 score  LinearRegression on train is:", r2_score(y_train, y_train_pred_lr)

    #plt.scatter(X_train, y_train, label='Train Samples')
#岭回归
def fun_RidgeRegression():
    alphas = [0.01, 0.1, 10,100]
    lr = RidgeCV(alphas=alphas, store_cv_values=True)
    lr.fit(x_train,y_train)
    y_test_pred_lr = lr.predict(x_test)
    y_train_pred_lr = lr.predict(x_train)
    #测试集
    print "the r2 score   on reg  test is:", r2_score(y_test, y_test_pred_lr)
    #训练集
    print "the r2 score   on reg  train is:", r2_score(y_train, y_train_pred_lr)

if __name__ == '__main__':
	#无正则的线性回归
    #fun_OLS()
	
	#岭回归
    fun_RidgeRegression()